package com.itheima.tanhua.service;

import cn.hutool.core.collection.CollUtil;
import com.itheima.autoconfig.template.HuanXinTemplate;
import com.itheima.tanhua.api.db.UserInfoApi;
import com.itheima.tanhua.api.db.UserServiceApi;
import com.itheima.tanhua.api.mongo.FriendApi;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.pojo.db.user;
import com.itheima.tanhua.pojo.mongo.Friend;
import com.itheima.tanhua.until.mongo.Constants;
import com.itheima.tanhua.vo.HuanxinVo.ContactVo;
import com.itheima.tanhua.vo.mongo.UserInfoVo;
import com.itheima.tanhua.vo.mongo.pageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.tanhua.interceptor.userHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 芝士雪豹
 * @create 2022/9/28/17:44
 */
@Service
public class MessagesService {
    @Autowired
    private UserServiceApi userServiceApi;
    @Autowired
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;

    //根据环信id 查询用户信息
    public UserInfoVo findUserinfoByHuanxin(String huanxinId) {
        //根据环信id查询用户基本信息
        user user = userServiceApi.findByHxId(huanxinId);
        //根据用户id查询用户资料
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //构建vo对象
        UserInfoVo vo = new UserInfoVo();
        //拷贝对象,age已经做过处理了
        BeanUtils.copyProperties(userInfo, vo);
        return vo;
    }

    //添加好友
    public void contacts(Long friendId) {
        //调用Templ 判断是否添加成功
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userHolder.getId(),
                Constants.HX_USER_PREFIX + friendId);
        //添加成功后 记录到朋友关系到mongo
        if (aBoolean) {
            friendApi.save(userHolder.getId(), friendId);
        }

    }

    //查询联系人列表
    public pageResult findByFriend(Integer page, Integer pagesize, String keyword) {
        //调用api查询当前用户好友信息
        List<Friend> friends = friendApi.findByUserId(userHolder.getId(), page, pagesize);
        if (friends == null) {
            return null;
        }
        //提取好友中的好友id
        List<Long> friendId = CollUtil.getFieldValues(friends, "friendId", Long.class);
        //调用api 查询好友的用户详情
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userServiceApi.findByIds(friendId, userInfo);
        //构建vo对象
        ArrayList<ContactVo> contactVos = new ArrayList<>();
        for (Friend friend : friends) {
            UserInfo userInfo1 = map.get(friend.getFriendId());
            if (userInfo1!=null){
                ContactVo vo = ContactVo.init(userInfo1);
                contactVos.add(vo);
            }
        }
        return new pageResult(page,pagesize,contactVos);
    }
}
